<template>
  <el-dialog :title="dialogTitle" :visible.sync="handleDialog" :before-close="closeDialog" :close-on-click-modal="false" width="500px">
    <el-form ref="sourceForm" :model="sourceForm" :rules="rules">
      <el-form-item label="订单来源名称：" label-width="140px" prop="source_name">
        <el-input v-model="sourceForm.source_name" autocomplete="off" />
      </el-form-item>
      <template v-if="isEdit">
        <el-form-item label="来源标识：" label-width="140px" prop="source_code">
          <el-input v-model="sourceForm.source_code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态:" label-width="130px" prop="status">
          <el-radio-group v-model="sourceForm.status">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submitForm('sourceForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import cropUpload from '@/components/cropUpload'
export default {
  components: { cropUpload },
  props: {
    dialogTitle: {
      type: String,
      default: ''
    },
    handleDialog: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    sourceForm: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    var checkCode = (rule, value, callback) => {
      const reg = /^(?!\d+$)[\da-zA-Z]+$/
      if (!value) {
        callback(new Error('请输入来源标识'))
      } else if (!reg.test(value) || value.length > 20) {
        callback(new Error('请输入20位以内英文或加数字组合'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        source_name: [
          { required: true, message: '请输入订单来源名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        source_code: [
          { required: true, message: '请输入来源标识', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    closeDialog(param) {
      this.$emit('closeDialog', param)
    },
    setUrl(val) {
      this.$emit('setUrl', val)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submitForm', true)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
</style>
